package com.qf.userinformationsystem.handler;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.userinformationsystem.pojo.UserInfo;
import com.qf.userinformationsystem.service.UserInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("userInfo")
public class UserInfoHandler {
    @Resource
    UserInfoService userInfoService;

    @GetMapping("getAll")
    public PageInfo<UserInfo> users(@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        PageHelper.startPage(pn,10);
        System.out.println("分页查询控制器");
        List<UserInfo> all = userInfoService.getAll();
        System.out.println(userInfoService == null);
        System.out.println(all);
        PageInfo<UserInfo> pageInfo = new PageInfo<>(all,10); // navigatePages：一次性要显示多少页
        return pageInfo;
    }

    @GetMapping("showAccount")
    public String showAccount(HttpServletRequest httpServletRequest){
        Object account = httpServletRequest.getSession().getAttribute("account");
        return (String) account;
    }

    @PostMapping("add")
    public String add(@RequestBody UserInfo userInfo,HttpServletRequest httpServletRequest){
        List<UserInfo> userByIdNum = userInfoService.getUserByIdNum(userInfo.getIdNum());
        Object account = httpServletRequest.getSession().getAttribute("account");
        Date date=new Date();

        if(userByIdNum.isEmpty()){
            userInfo.setCreator((String)account);
            userInfo.setCreateTime(date);
            int insert = userInfoService.insert(userInfo);
            return insert==1?"Y":"N";
        }else {
            return "X";
        }
    }

    @PostMapping("edit")
    public String edit(@RequestBody UserInfo userInfo,HttpServletRequest httpServletRequest){
        List<UserInfo> userByIdNum = userInfoService.getUserByIdAndIdNum(userInfo.getId(),userInfo.getIdNum());
        Object account = httpServletRequest.getSession().getAttribute("account");
        Date date=new Date();

        if(userByIdNum.isEmpty()){
            userInfo.setUpdator((String) account);
            userInfo.setUpdateTime(date);
            int update = userInfoService.update(userInfo);
            return update==1?"Y":"N";
        }else {
            return "X";
        }
    }

    @GetMapping("exit")
    public void exit(HttpServletRequest httpServletRequest){
        httpServletRequest.getSession().invalidate();
    }

    @GetMapping("searchByFour")
    public List<UserInfo> searchByFour(String name,Integer age,String sTime,String eTime){
        List<UserInfo> userInfos = userInfoService.searchByFour(name, age, sTime, eTime);
        return  userInfos;
    }

    @PostMapping("delete")
    public String delete(Integer id,String name,HttpServletRequest httpServletRequest){
        Object account = httpServletRequest.getSession().getAttribute("account");
        String name1=(String) account;
        System.out.println(name1);

        if(name1.equals(name)){
            return "X";
        }else {
            int delete = userInfoService.delete(id, name1);
            return delete==1?"Y":"N";
        }
    }

    @PostMapping("delBatch")
    public String delBatch(String ids,HttpServletRequest httpServletRequest){

        Object account = httpServletRequest.getSession().getAttribute("account");
        String name1=(String) account;

        List<UserInfo> userByIds = userInfoService.getUserByIds(ids);
        for (int i = 0; i < userByIds.size(); i++) {
            if(userByIds.get(i).getName().equals(name1)){
                return "X";
            }
        }

        String[] split = ids.split(",");
        int i = userInfoService.delBatch(ids,name1);
        return i==split.length?"Y":"N";
    }

    @PostMapping("count")
    public List<Integer> count(){
        List<Integer> count = userInfoService.count();
        return count;
    }
}
